import time
import curses

def main(stdscr):
    # while True:
    #     time.sleep(1)

    curses.use_default_colors()
    curses.cbreak()
    stdscr.keypad(True)
    stdscr.nodelay(True)  # 非阻塞模式
    
    print("按键检测中: q退出 - 消息打印在终端/日志")
    
    while True:
        c = stdscr.getch()
        if c != -1:  # -1 表示无按键
            # 特殊按键处理
            if c == curses.KEY_UP: key = "↑"
            elif c == curses.KEY_DOWN: key = "↓"
            elif c == curses.KEY_LEFT: key = "←"
            elif c == curses.KEY_RIGHT: key = "→"
            elif c == 10: key = "Enter"
            elif c == 27: key = "ESC"
            elif c == 113:  # 'q'
                break
            else: key = chr(c)
            
            print(f"检测到按键: {key}")
        
        # 在此添加你的主要程序逻辑
        time.sleep(0.05)  # 释放 CPU

if __name__ == "__main__":
    curses.wrapper(main)
    print("程序已退出")